﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Menu.aspx.cs" Inherits="XYTXL.View.Menu" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>菜单管理</title>
    <link href="/View/CSS/bootstrap3.3.5/css/bootstrap.css" rel="stylesheet" />
    <link rel="stylesheet" type="text/css" href="/View/CSS/Role.css" />
    <link rel="stylesheet" type="text/css" href="/View/JS/zTreeStyle/zTreeStyle.css" />
    <link rel="stylesheet" type="text/css" href="/View/JS/smartMenu/css/smartMenu.css" />
    <script src="/View/JS/jquery-1.11.2.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="/View/JS/zTreeStyle/jquery-ztree-2.5.min.js"></script>
    <script type="text/javascript" src="/View/JS/smartMenu/js/jquery-smartMenu.js"></script>
    <script type="text/javascript">
        var zTree1, rMenu, setting, BtnMenuData;
        function getMenu() {
            setting = {
                isSimpleData: true,
                treeNodeKey: "id",
                treeNodeParentKey: "pId",
                showLine: false,
                callback: {
                    rightClick: zTreeOnRightClick,
                    click: zTreeOnClick

                },
                root: {
                    isRoot: true,
                    nodes: []
                }
            };
            $.ajax({
                type: "GET",
                url: "AdminHandle.ashx",
                dataType: "html",
                data: { "Action": "GetAllAuth" },
                beforeSend: function (XMLHttpRequest) {
                    parent.MyDialog.Mytips();
                },
                success: function (msg) {
                    zNodes = eval(msg);
                    zTree1 = $("#tree").zTree(setting, zNodes);
                },
                complete: function (XMLHttpRequest, textStatus) {
                    parent.MyDialog.CloseMytips();

                },
                error: function () {
                    alert('error');
                }
            });
        }
        function showRMenu(type, x, y) {
            $("#MenuQX").show();
            $("#MenuQX").css({ "top": y + "px", "left": x + "px", "display": "block" });
        }
        function hideRMenu() {
            $("#MenuQX").hide();
        }

        function zTreeOnClick() {
            //初始值
            var node = zTree1.getSelectedNode();

            //如果选中节点有对应的路径，则显示管理按钮功能
            $("#MangerMenu").show('normal');
            //if ($.trim(node.taburl) === "") {
            //    $("#divButton").hide();

            //}
            //else {
            //    $("#divButton").show();
            //    $("#divButton  div[class='topic-item'][pauthcode!='" + node.id + "']").hide();
            //    $("#divButton  div[class='topic-item'][pauthcode='" + node.id + "']").show();
            //}

            //如果选中节点有对应的路径，则显示管理按钮功能

            //更新
            $("#upMenuCode").val(node.id);
            $("#MangerMenuTitle").text(node.name);
            $("#tbMenuName").val(node.name);
            $("#tbMenuDisOrder").val(node.order);
            $("#tbMenuFileName").val(node.taburl);

            $("#tbMenuImg").val(node.menuicon);
            $("#ckIsUse").attr("checked", node.isuse == "Y" ? true : false);
            $("#ckISHasBt").attr("checked", node.ishasbt == "Y" ? true : false);



        }
        function zTreeOnRightClick(event, treeId, treeNode) {
            if (!treeNode) {
                zTree1.cancelSelectedNode();
                showRMenu("root", event.clientX, event.clientY);
            } else if (treeNode && !treeNode.noR) {
                zTree1.selectNode(treeNode);
                showRMenu("bran", event.clientX, event.clientY);
            }
        }
        function ManageMenu(action) {
            var node = zTree1.getSelectedNode();
            var moveType = "inner";
            if (action == 'add') {
                $("#MangerMenu").hide();
                $("#MangerMenu").show('normal');

                //初始值
                $("#upMenuCode").val(""); //初始化需要更新菜单的值
                $("#MangerMenuTitle").text('增加菜单');
                $("#tbMenuName").val("");

                $("#tbMenuDisOrder").val("");
                $("#tbMenuFileName").val("");
                $("#tbMenuImg").val("");
                //隐藏菜单
                hideRMenu();

            }
            if (action == 'del') {
                if (node) {
                    parent.MyDialog.confirm('删除菜单会删除所有该菜单项下的所有子菜单,你确定要删除该菜单吗', function () {
                        delAuth(node);
                    });
                }
                hideRMenu();

            }
            if (action == "up") {
                moveType = "before";
                targetNode = getPreTreeNode(node);
                if (node && targetNode) {
                    zTree1.moveNode(targetNode, node, moveType);
                }
            }
            if (action == "down") {
                moveType = "after";
                targetNode = getNextTreeNode(node);
                if (node && targetNode) {
                    zTree1.moveNode(targetNode, node, moveType);
                }
            }
        }

        //删除菜单
        function delAuth(node) {
            $.ajax({
                type: "GET",
                url: "AdminHandle.ashx",
                dataType: "html",
                data: { "Action": "DelMenu", "MenuID": node.id },
                success: function (msg) {
                    if (true) {
                        parent.MyDialog.tips("删除成功");

                        zTree1.removeNode(node);
                        //创建按钮副本

                    }
                }
            });
        }
        function getPreTreeNode(treeNode) {
            if (treeNode.isFirstNode) return null;
            var nodes = treeNode.parentNode ? treeNode.parentNode.nodes : zTree1.getNodes();
            var preNode;
            for (var i = 0; i < nodes.length; i++) {
                if (nodes[i] == treeNode) {
                    return preNode;
                }
                preNode = nodes[i];
            }
        }
        function getNextTreeNode(treeNode) {
            if (treeNode.isLastNode) return null;
            var nodes = treeNode.parentNode ? treeNode.parentNode.nodes : zTree1.getNodes();
            for (var i = 0; i < nodes.length; i++) {
                if (nodes[i] == treeNode) {
                    return nodes[i + 1];
                }
            }
        }

        $(document).ready(function () {

            getMenu();
            rMenu = $("#MenuQX");
            //鼠标不在菜单中按下时隐藏菜单
            $("body").bind("mousedown",
            function (event) {
                if (!(event.target.id == "MenuQX" || $(event.target).parents("#MenuQX").length > 0)) {
                    $("#MenuQX").hide();
                }
            });
            $("#btConfirm").click(function (e) {
                //增加菜单
                var node = null;
                var Action = null;
                var nodeid = null;
                var nodelever = null;
                if ($("#upMenuCode").val() == "") {
                    node = zTree1.getSelectedNode();
                    Action = "AddAuth";
                    nodeid = node.id;
                    nodelever = node.level;
                }
                else {
                    nodeid = $("#upMenuCode").val();
                    Action = "UpdateAuth";
                }

                var nodename = $("#tbMenuName").val();
                var nodeorder = $("#tbMenuDisOrder").val();

                var nodeurl = $("#tbMenuFileName").val();
                var nodeico = $("#tbMenuImg").val();
                var nodeisuse = $("#ckIsUse").attr("checked") ? "Y" : "N";
                var nodeishasbt = $("#ckISHasBt").attr("checked") ? "Y" : "N";


                $.ajax({
                    type: "GET",
                    url: "AdminHandle.ashx",
                    dataType: "html",
                    data: { "Action": Action, "nodeid": nodeid, "nodelever": nodelever, "nodename": nodename, "nodeorder": nodeorder, "nodeurl": nodeurl, "nodeico": nodeico, "nodeisuse": nodeisuse, "nodeishasbt": nodeishasbt },

                    success: function (msg) {
                        if (msg == "error")
                        {
                            parent.MyDialog.tips("操作失败")
                            return;
                        }
                        parent.MyDialog.tips("操作成功")
                        newNode = eval(msg);
                        if (Action == "AddAuth") {
                            zTree1.addNodes(node, newNode);
                        }
                        else {
                            var UPnode = zTree1.getNodeByParam("id", nodeid);
                            UPnode.taburl = nodeurl;
                            UPnode.name = nodename;
                            UPnode.menuicon = nodeico;
                            UPnode.isuse = nodeisuse;
                            UPnode.ishasbt = nodeishasbt;
                            UPnode.order = nodeorder;
                            zTree1.updateNode(UPnode, true);
                        }
                    }
                });
            });
            //

            //为菜单添加按钮
            $("#divaddbutton").click(function (e) {
                var obj = $("#MangerButton").html();
                var node = zTree1.getSelectedNode();

                MyDialog.btview('添加按钮', obj, '确认添加', function () {
                    var btnid = $("#tbButtonID").val();
                    var btndec = $("#tbButtonDec").val();
                    $.ajax({
                        type: "GET",
                        url: "AdminHandle.ashx",
                        dataType: "html",
                        data: { "Action": "AddMenuButton", "MenuID": node.id, "nodelever": node.level, "btnid": btnid, "btndec": btndec },
                        success: function (msg) {
                            MyDialog.tips("添加按钮成功", function () {
                                //创建按钮副本
                                var newNode = eval(msg);
                                var newbt = $("div[btnid='buttonmoban']").clone().attr("btnid", btnid).attr("btndec", btndec).attr("btncode", newNode[0].id);
                                newbt.smartMenu(BtnMenuData, {
                                    name: "BtnMenu"
                                });
                                $("span", newbt).text(btndec);
                                newbt.prependTo($("#divButton")).show('normal');
                                //创建按钮副本
                            })
                        }
                    });
                });
            });

            //角色单击事件
            BtnMenuData = [
                [{
                    text: "删除按钮.",
                    func: function () {
                        var btncode = $(this).attr('btncode');
                        var objbtn = $(this);
                        $.ajax({
                            type: "GET",
                            url: "AdminHandle.ashx",
                            dataType: "html",
                            data: { "Action": "DelMenu", "MenuID": btncode },
                            success: function (msg) {
                                if (true) {
                                    parent.MyDialog.tips("删除成功")
                                    objbtn.remove();

                                }
                            }
                        });
                    }
                }, {
                    text: "修改按钮.",
                    func: function () {
                        var obj = $("#MangerButton").html();
                        var objbtn = $(this);
                        var btncode = objbtn.attr('btncode');
                        MyDialog.btview('修改按钮', obj, '确认修改', function () {
                            var btnid = $("#tbButtonID").val();
                            var btndec = $("#tbButtonDec").val();
                            $.ajax({
                                type: "GET",
                                url: "AdminHandle.ashx",
                                dataType: "html",
                                data: { "Action": "UpdateMenuBtn", "btncode": btncode, "btnid": btnid, "btndec": btndec },
                                success: function (msg) {
                                    parent.MyDialog.tips("修改成功")
                                    objbtn.attr("btnid", btnid).attr("btndec", btndec);
                                    $("span", objbtn).text(btndec);

                                }
                            });
                        });
                        //初始化修改窗口
                        $("#tbButtonID").val(objbtn.attr('btnid'));
                        $("#tbButtonDec").val(objbtn.attr('btndec'));
                    }
                }],
                [{
                    text: "复制按钮.",
                    func: function () {
                        var btncode = $(this).attr('btncode');
                        var pageurl = 'CopyBtn.aspx?btncode=' + btncode;
                        MyDialog.viewform(pageurl, '复制按钮', 850, 500);
                    }
                }]
            ];
            //$("#divButton .topic-item").smartMenu(BtnMenuData, {
            //    name: "BtnMenu"
            //});
            //角色单击事件

        })

    </script>
</head>
<body>
    <form id="form1" runat="server">
        <table id="container">
            <tr>
                <td valign="top">
                    <div id="sidebar" class="well" style="margin: 20px;">
                        <ul id="tree" class="tree">
                        </ul>
                    </div>

                </td>
                <td valign="top" style="width: 80%">

                    <div id="MangerMenu" style="margin: 20px; display: none">
                        <div id="content" class="well">

                            <form class="form-horizontal">
                                <div class="control-group">
                                    <label class="control-label" for="tbMenuName">权限节点名称</label>
                                    <div class="controls">
                                        <asp:TextBox ID="tbMenuName" class="span4" runat="server" Title="菜单节点名称不能为空，不能重复" Text="" />
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label" for="tbMenuFileName">对应执行文件</label>
                                    <div class="controls">
                                        <asp:TextBox ID="tbMenuFileName" class="span4" runat="server" Title="对应执行文件" Text="" />
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label" for="tbMenuDisOrder">对应顺序</label>
                                    <div class="controls">
                                        <asp:TextBox ID="tbMenuDisOrder" class="span4" runat="server"
                                            Title="对应顺序" Text="" />
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label" for="tbMenuImg">权限对应图标</label>
                                    <div class="controls">
                                        <asp:TextBox ID="tbMenuImg" class="span4" runat="server" Title="菜单对应图标"
                                            Text="" />
                                    </div>
                                </div>
                                <div class="control-group">
                                    <div class="controls">
                                        <label class="checkbox">
                                            <asp:CheckBox ID="ckIsUse" runat="server" Text="是否可用" Checked="true" />

                                        </label>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <div class="controls">
                                        <label class="checkbox">
                                            <asp:CheckBox ID="ckISHasBt" runat="server" Text="是否添加默认按钮" Checked="true" />

                                        </label>
                                    </div>
                                </div>
                                <div id="divButton" class="well" style="display: none">
                                    <asp:Repeater ID="RptRole" runat="server">
                                        <ItemTemplate>
                                            <div class="topic-item" style="display: none" btndec="<%#Eval("AuthName") %>" btnid="<%#Eval("FileName") %>"
                                                btncode="<%#Eval("AuthCode") %>" pauthcode="<%#Eval("PAuthCode") %>">
                                                <div class="topic-img">
                                                    <img src="/View/images/ic_popup_video.png" style="width: 64px; height: 64px;">
                                                </div>
                                                <div class="topic-title">
                                                    <span style="color: #4987C9">
                                                        <%#DataBinder.Eval(Container.DataItem, "AuthName")%></span>
                                                </div>
                                            </div>
                                        </ItemTemplate>
                                    </asp:Repeater>
                                    <div class="topic-itemAdd" style="margin-bottom: 20px;" id="divaddbutton">
                                        <div class="topic-img">
                                            <img src="/View/images/ic_popup_new.png" style="width: 64px; height: 64px;">
                                        </div>
                                        <div class="topic-title">
                                            <span style="color: red;">创建新按钮</span>
                                        </div>
                                    </div>
                                </div>
                                <input type="button" id="btConfirm" value="确认保存" class="btn-success btn btn-large" style="margin-left: 5px" />
                            </form>

                        </div>


                    </div>
                </td>
            </tr>
        </table>
        <div id="MenuQX" class="smart_menu_box" style="display: none;">
            <div class="smart_menu_body">
                <ul class="smart_menu_ul">
                    <li class="smart_menu_li" onclick="ManageMenu('add')"><a href="javascript:" class="smart_menu_a">增加菜单</a></li>
                    <li class="smart_menu_li" onclick="ManageMenu('del')"><a href="javascript:" class="smart_menu_a">删除菜单</a></li>
                    <li class="smart_menu_li_separate">&nbsp;</li>
                    <li class="smart_menu_li" onclick="ManageMenu('up')"><a href="javascript:" class="smart_menu_a">上移</a></li>
                    <li class="smart_menu_li" onclick="ManageMenu('down')"><a href="javascript:" class="smart_menu_a">下移</a></li>
                </ul>
            </div>
        </div>
        <div id="MangerButton" style="display: none">
            <div class="container-fluid">
                <fieldset>
                    <label>按钮ID</label>
                    <asp:TextBox ID="tbButtonID" class="span2" runat="server" Title="按钮ID,不能为空"
                        Text="" />
                    <label>按钮描述</label>
                    <asp:TextBox ID="tbButtonDec" runat="server" CssClass="span2" Text="" Title="按钮描述" />

                </fieldset>
            </div>
        </div>
        <div class="topic-item" style="margin-bottom: 20px; display: none" btnid="buttonmoban">
            <div class="topic-img">
                <img src="/View/images/ic_popup_video.png" style="width: 64px; height: 64px;">
            </div>
            <div class="topic-title">
                <span></span>
            </div>
        </div>
        <input type="hidden" id="upMenuCode" value="" runat="server" />
    </form>
</body>
</html>
